# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

source('cycles.R')
source('metrics.R')

print.stats <- function(name, stats)
{
	print(paste(name, " #v:", stats[["nv"]]))
	print(paste(name, " #e:", stats[["ne"]]))
	print(paste(name, " density:", stats[["dens"]]))
	print(paste(name, " CPL:", stats[["cpl"]]))
	print(paste(name, " diameter (d):", stats[["ddiam"]]))
	print(paste(name, " diameter (u):", stats[["udiam"]]))
	print(paste(name, " avg deg:", stats[["avgdeg"]]))
	print(paste(name, " avg pr:", stats[["avgpr"]]))
	print(paste(name, " transitivity:", stats[["gtrans"]]))
}

print.short.cycles.stats <- function(name, c2, c3, c3b)
{
	print(paste(name, " # 2-cycles:", length(c2$c)))
	print(paste(name, " # 3-shortest-cycles:", length(c3$c)))
	print(paste(name, " # 3-cycles:", length(c3b$c)))
	
	print(paste(name, " #v on 2-cycles:", length(c2$v)))
	print(paste(name, " #e on 2-cycles:", length(c2$e)))
	
	print(paste(name, " #v on 3-shortest-cycles:", length(c3$v)))
	print(paste(name, " #e on 3-shortest-cycles:", length(c3$e)))
	print(paste(name, " #v on 3-cycles:", length(c3b$v)))
	print(paste(name, " #e on 3-cycles:", length(c3b$e)))
	
}

print.3mult.entropy <- function(name, c3)
{
	ptvfa <- prop.table(c3$vcm)
	print(paste(name, " 3-vertex-multiplicity entropy:", get.entropy(ptvfa)))
	print(paste(name, " 3-vertex-multiplicity normalized entropy:", get.entropy(ptvfa) / log(length(ptvfa))))
	
	ptefa <- prop.table(c3$ecm)
	print(paste(name, " 3-edge-multiplicity entropy:", get.entropy(ptefa)))
	print(paste(name, " 3-edge-multiplicity normalized entropy:", get.entropy(ptefa) / log(length(ptefa))))
}

fa <- read.graph(file = "fa-core-graphml.xml", format = "graphml")
fa <- simplify(fa)

# cpl: characteristic path length
# msccs: main SCC size
# mwccs: main WCC size
# ddiam: directed diameter
# udiam: undirected diameter
# eff: efficiency
# assort: assortativity
# gtrans: global transitivity (global CC)
# altrans: avg of local transitivities (avg of local CCs)
# dens: density
# nv: # vertices
# ne: # edges
# avgpr: avg pagerank
# avgdeg: avg degree
# avgindeg
# avgoutdeg
# maxdeg
# maxindeg
# maxoutdeg
# mindeg
# minindeg
# minoutdeg 
metrics <- c("cpl", "ddiam", "udiam", "dens", "nv", "ne", "avgdeg", "avgpr", "gtrans")
stats <- get.graph.stats(fa, metrics)

print.stats("Core", stats)

# vids: vector of vertex names
# eids: vector of edge names
# cycles: vector of cycles ids (concatenated set of vertex ids)
# cnames: vector of cycle names (concatenated set of vertex names)
# vcm: named vector (vertex name -> multiplicity)
c2 <- get.shortest.cycles(fa,2)
c3 <- get.shortest.cycles(fa,3)
c3b <- get.cycles(fa,3)

print.short.cycles.stats("Core" , c2, c3, c3b)

v23 <- intersect(c2$v, c3$v)
e23 <- intersect(c2$e, c3$e)

v23b <- intersect(c2$v, c3b$v)
e23b <- intersect(c2$e, c3b$e)

print(paste("Core #v on 2 inter 3-shortest-cycles:", length(v23)))
print(paste("Core #e on 2 inter 3-shortest-cycles:", length(e23)))

print(paste("Core #v on 2 inter 3-cycles:", length(v23b)))
print(paste("Core #e on 2 inter 3-cycles:", length(e23b)))

print.3mult.entropy("Core", c3b)

### seed ###

seed <- get.subgraph(fa,c3b$v)
write.graph(seed, file= "fa-seed-graphml.xml", format = "graphml")

seedc <- clusters(seed, mode = "strong")
print(paste("Seed # SCCs:", seedc$no))
print(paste("Seed main SCC size:", max(seedc$csize)))

stats3b <- get.graph.stats(seed, metrics)
print.stats("Seed", stats3b)

seedc2 <- get.shortest.cycles(seed,2)
seedc3 <- get.shortest.cycles(seed,3)
seedc3b <- get.cycles(seed,3)

print.short.cycles.stats("Seed" , seedc2, seedc3, seedc3b)
print.3mult.entropy("Seed", seedc3b)

### shell
shell <- delete.vertices(fa, c3b$v)
write.graph(shell, file= "fa-shell-graphml.xml", format = "graphml")

shellc <- clusters(shell, mode = "strong")
print(paste("Shell # SCCs:", shellc$no))
print(paste("Shell main SCC size:", max(shellc$csize)))

svids <- get.main.cc.vertex.indexes(shell, mode = "weak")
shellmwcc <- subgraph(shell, svids)

write.graph(shellmwcc, file= "fa-shell-main-wcc-graphml.xml", format = "graphml")
write.graph(shellmwcc, file= "fa-shell-main-wcc-gml.gml", format = "gml")

stats4b <- get.graph.stats(shell, metrics)
print.stats("Shell", stats4b)

shellc2 <- get.shortest.cycles(shell,2)
shellc3 <- get.shortest.cycles(shell,3)
shellc3b <- get.cycles(shell,3)

print.short.cycles.stats("Shell" , shellc2, shellc3, shellc3b)
print.3mult.entropy("Shell", shellc3b)

### equivalent ER ###

# FA-ER

#faer <- erdos.renyi.game(4843, 61544, type = "gnm", directed = TRUE)
#V(faer)$id <- paste(c(1:length(V(faer))), sep = "")
#E(faer)$id <- paste(c(1:length(E(faer))), sep = "")

#faerc2 <- get.shortest.cycles(faer,2)
#faerc3 <- get.shortest.cycles(faer,3)
#faerc3b <- get.cycles(faer,3)

#statsfaer <- get.graph.stats(faer, metrics)
#print.stats("FA-ER", statsfaer)

#print.short.cycles.stats("FA-ER" , faerc2, faerc3, faerc3b)
#print.3mult.entropy("FA-ER", faerc3b)

# Seed-ER

seeder <- erdos.renyi.game(4313, 54197, type = "gnm", directed = TRUE)
V(seeder)$id <- paste(c(1:length(V(seeder))), sep = "")
E(seeder)$id <- paste(c(1:length(E(seeder))), sep = "")

seederc2 <- get.shortest.cycles(seeder,2)
seederc3 <- get.shortest.cycles(seeder,3)
seederc3b <- get.cycles(seeder,3)

statsseeder <- get.graph.stats(seeder, metrics)
print.stats("Seed-ER", statsseeder)

print.short.cycles.stats("Seed-ER" , seederc2, seederc3, seederc3b)
print.3mult.entropy("Seed-ER", seederc3b)
